<?php
/***
 * 优惠
 *
 *
 * @author    lanhongjie <lanhj@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */



namespace App\Services\PreferentialService;


use App\Services\ProductTempService\ProductTempTool;
use App\Services\SourcePicService\SourcePicTool;
use Illuminate\Support\Facades\DB;
use App\Services\FontService\FontService;
class PreferentialService
{

    /**
     * PreferentialService constructor.
     */
    public function __construct()
    {
    }

    /***
     * 查看某一资源当前是否有优惠
     * @param $resources_id
     * @param $resources_type
     * @return bool
     */
    public static function resourcesHasPreferential($resources_id,$resources_type,$colour_id=0){
       if(empty($resources_id)||empty($resources_type)){
           return false;
       }
       $time=time();
        $num=DB::table('preferentials')->where(array(
            'state'=>1,
            'resources_id'=>$resources_id,
            'resources_type'=>$resources_type
         ))->where('stock','>',0)
           ->where('begin_time','<=',$time)
           ->where('end_time','>',$time)
           ->count();
        return ($num>0)?true:false;
    }

    /***
     * 获取资源价钱
     * @param $resources_id
     * @param $resources_type  1 素材 2 模板 3字体
     * @param int $colour_id
     * @return array|bool
     */
    public static function getResourcePrice($resources_id,$resources_type,$colour_id=0){
        if(empty($resources_id)||empty($resources_type)){
            return false;
        }
        $time=time();
        $preferential=DB::table('preferentials')->where(array(
            'state'=>1,
            'resources_id'=>$resources_id,
            'resources_type'=>$resources_type
        ))->where('stock','>',0)
            ->where('begin_time','<=',$time)
            ->where('end_time','>',$time)
            ->select('preferential_price','stock')
            ->first();
        $original_price=0;
        switch ($resources_type) {
            case 1:
                $original_price=SourcePicTool::getSourcePicPrice($resources_id,0);
                break;
            case 2:
                $original_price=ProductTempTool::getProdTempPrice($resources_id,$colour_id,0);
                break;
            case 3:
                $fontService=new FontService();
                $original_price=$fontService->getFontPrice($resources_id);
                break;
            default :
                return false;
        }
        if($preferential===null){
            $preferential_price=$original_price;
            $preferential_stock=0;
        }else{
            $preferential_price=$preferential->preferential_price;
            $preferential_stock=$preferential->stock;
        }
        return array('preferential_price'=>$preferential_price,'preferential_stock'=>$preferential_stock,'original_price'=>$original_price);
    }
    /***
     * 某一类型下当前优惠资源Id数组
     * @param $resources_type
     * @return array
     */
    public static function resourceIdsCurrentOffersByType($resources_type){
        if(empty($resources_type)||empty($resources_type)){
            return array();
        }
        $time=time();
        $resources_ids=DB::table('preferentials')->where(array(
            'state'=>1,
            'resources_type'=>$resources_type
        ))->where('stock','>',0)
            ->where('begin_time','<=',$time)
            ->where('end_time','>',$time)
            ->pluck('resources_id');
        return $resources_ids;
    }

    /***
     *  优惠列表中删除某一优惠资源
     * @param $resources_id
     * @return bool
     */
    public static function delResourceByResourceId($resources_id){
        if(empty($resources_id)){
            return false;
        }
        $time=time();
        $update_res=DB::table('preferentials')->where(array(
            'state'=>1,
            'resources_type'=>$resources_id
        ))->update(array(
            'state'=>0,
            'resources_type'=>$time
        ));
        return ($update_res==0)?false:true;
    }
    /***
     * 优惠记录
     * @param $id
     * @return null
     */
    public static function getPreferentialById($id){
        if(empty($id)){
            return null;
        }
        $preferential=DB::table('preferentials')->where(array(
            'state'=>1,
            'resources_id'=>$id,
        ))->first();
        return $preferential;
    }
}